package com.app.service.algs;

import com.app.domain.UploadParameter;
import com.app.domain.YjData;
import com.app.domain.wxjjlb.CompressSlurry;
import com.app.domain.wxjjlb.CompressSlurryData;
import com.app.domain.wxjjlb.CompressSlurryHole;
import com.app.service.UploadParameterService;
import com.app.service.YjDataService;
import com.app.utils.MD5Utils;
import com.app.utils.TimeHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
@Slf4j
public class WxjjlbService {
    @Autowired
    UploadParameterService uploadParameterService;
    @Autowired
    YjDataService yjDataService;

    public CompressSlurry getYjdata(List<YjData> yjDataList, UploadParameter uploadParameter, String appKey, String appSecret, Map<String, Object> map) throws NoSuchAlgorithmException, ParseException {
        CompressSlurry compressSlurry = new CompressSlurry();
        compressSlurry.setAppKey(appKey);
        String timestamp = String.valueOf(System.currentTimeMillis());
        compressSlurry.setTimestamp(timestamp);
        String md532bit = MD5Utils.MD5_32bit(appKey + timestamp + appSecret);
        compressSlurry.setSign(md532bit);
        CompressSlurryData compressSlurryData = getCompressSlurryData(yjDataList, uploadParameter,map);
        compressSlurry.setData(compressSlurryData);
        return compressSlurry;
    }

    private CompressSlurryData getCompressSlurryData(List<YjData> yjDataList, UploadParameter uploadParameter, Map<String, Object> map) throws ParseException {
        CompressSlurryData compressSlurryData = new CompressSlurryData();
        YjData yjData = yjDataList.get(0);
        compressSlurryData.setBeamNumCode(yjData.getBridge_code());
        compressSlurryData.setDeviceCode("2023040901");
        compressSlurryData.setLabId("2023040901");
        compressSlurryData.setProjectPart(uploadParameter.getSgbw());
        compressSlurryData.setBridgeName(uploadParameter.getEquipmentname());
        compressSlurryData.setCsOrder(uploadParameter.getYajiangbuzhou());
        compressSlurryData.setCsDirection(uploadParameter.getYajiangfangxiang());
        compressSlurryData.setExecTime(TimeHelper.transToUTCDate(yjData.getMudjack_date() + " " + map.get("beginTime")) );
        compressSlurryData.setDesignStrength(uploadParameter.getConcretestrength());
        compressSlurryData.setBeamType(uploadParameter.getLblx());
        compressSlurryData.setMaterialFactory("常规");
        compressSlurryData.setMaterialSpec("常规");
        compressSlurryData.setMaterialDesignValue(String.valueOf(yjData.getPractical_mudjack_value()));
        compressSlurryData.setMaterialActualValue(String.valueOf(yjData.getPractical_mudjack_value()));
        compressSlurryData.setCementDesignValue(String.valueOf(yjData.getPractical_water_value()));
        compressSlurryData.setCementActualValue(String.valueOf(yjData.getPractical_water_value()));
        compressSlurryData.setFlowSpec(yjData.getPractical_fluidity());
        compressSlurryData.setFlowInit(yjData.getPractical_fluidity());
        compressSlurryData.setFlow(yjData.getPractical_fluidity());
        compressSlurryData.setWaterDesignValue(String.valueOf(yjData.getPractical_water_value()));
        compressSlurryData.setWaterActualValue(String.valueOf(yjData.getPractical_water_value()));
        compressSlurryData.setWaterGlueRateDesignValue(String.valueOf(yjData.getWater_binder_ratio()));
        compressSlurryData.setWaterGlueRateActualValue(String.valueOf(yjData.getWater_binder_ratio()));
        compressSlurryData.setQualified(true);
        compressSlurryData.setHoleNum(String.valueOf(yjDataList.size()));
        compressSlurryData.setStructureCode(yjData.getBridge_code());
        List<CompressSlurryHole> compressSlurryHoleList = getCompressSlurryHole(yjDataList, uploadParameter);
        compressSlurryData.setHoleList(compressSlurryHoleList);
        return compressSlurryData;
    }

    private List<CompressSlurryHole> getCompressSlurryHole(List<YjData> yjDataList, UploadParameter uploadParameter) throws ParseException {
        List<CompressSlurryHole> compressSlurryHoleList = new ArrayList<>();
        for (YjData yjData : yjDataList) {
            CompressSlurryHole compressSlurryHole = new CompressSlurryHole();
            compressSlurryHole.setHoleCode(yjData.getDuct_code());
            compressSlurryHole.setCsDirection(uploadParameter.getYajiangfangxiang());
            compressSlurryHole.setCsOrder(uploadParameter.getYajiangbuzhou());
            compressSlurryHole.setAllocateRate(uploadParameter.getPhb());
            compressSlurryHole.setWaterGlueRate(String.valueOf(yjData.getWater_binder_ratio()));
            compressSlurryHole.setStartTime(TimeHelper.transToUTCDate(yjData.getMudjack_date() + " " + yjData.getBegin_time()));
            compressSlurryHole.setEndTime(TimeHelper.transToUTCDate(yjData.getMudjack_date() + " " + yjData.getEnd_time()));
            compressSlurryHole.setDesignSlurryVolume(String.valueOf(yjData.getPractical_mudjack_value()));
            compressSlurryHole.setCompressSlurryModel(uploadParameter.getYajiangmoshi());
            compressSlurryHole.setStirTime(uploadParameter.getStirtime());
            compressSlurryHole.setEntreSlurryPa(String.valueOf(yjData.getMudjack_pressure()));
            compressSlurryHole.setReturnSlurryPa(String.valueOf(yjData.getSlurry_pressure()));
            compressSlurryHole.setContinuedPaTime(yjData.getPressurize_time());
            compressSlurryHole.setEntreSlurryVolume(String.valueOf(yjData.getPractical_mudjack_value()));
            compressSlurryHole.setReturnSlurryVolume(String.valueOf(yjData.getPractical_mudjack_value()));
            compressSlurryHole.setCompressEnterCurve("");
            compressSlurryHole.setCompressReturnCurve("");
            compressSlurryHole.setCompressEnterPaCurve("");
            compressSlurryHole.setCompressReturnPaCurve("");
            compressSlurryHole.setContinuedEnterCurve("");
            compressSlurryHole.setContinuedReturnCurve("");
            compressSlurryHole.setContinuedEnterPaCurve("");
            compressSlurryHole.setContinuedReturnPaCurve("");
            compressSlurryHole.setQualified(true);
            compressSlurryHoleList.add(compressSlurryHole);
        }
        return compressSlurryHoleList;
    }
}
